<?php
if (!defined('IN_WONDERSHARE'))
{
	die('Hacking attempt');
}

class TemplateControl
{

	function __construct()
	{
	
	}
	
	/**
	* 更新作者模板
	* @param array $update_arr 需要更新的数组
	* @return boolean
	* */
	function updateAuthorTemplate( $update_arr) {
		
		$table_arr = array();
		$sum = count($GLOBALS['site_config']);
		
		$i = 0;
		foreach ($GLOBALS['site_config'] AS $key => $vlaues ) {
			
			$sql = "SELECT ar_id FROM topic_article 
					WHERE author_id = '{$update_arr['author_id']}' AND is_delete = 0 AND  web_type = '{$key}' ";
			$result = $GLOBALS['db']->query($sql);
			
			$ar_str_id = '';
			while ($row = $GLOBALS['db']->fetch_array($result)) {
				$ar_str_id .= ','.$row['ar_id'] ;
			}
			
			$ar_str_id = substr($ar_str_id, 1, str_len($ar_str_id));
			if (!empty($ar_str_id)) {
				
				$table_arr[$i] = array('table' => $vlaues['article']['table'], 'field_id' => $vlaues['article']['field_id'], 'id' => $ar_str_id);
				
				if( !empty($vlaues['article_single']['table']) ) {
					$table_arr[($sum + $i)] = array('table' => $vlaues['article_single']['table'], 'field_id' => $vlaues['article_single']['field_id'], 'id' => $ar_str_id);
				}
			}
			$i ++ ;
		}

		//发布或修改(修改时间)相关联文章模板
		$this->operation_template($update_arr, $table_arr);
	}
	
	/**
	* 更新标签模板
	* @param array $update_arr 需要更新的数组
	* @return boolean
	* */
	function updateTagTemplate( $update_arr) {
		
		$rows = $GLOBALS['site_config'][$update_arr['w_type']];

		
		 //以下是得到标签所关联的文章ID
 		$sql = "SELECT ar_id FROM topic_article 
 				WHERE ar_tag_id = '{$update_arr['tag_id']}' AND is_delete = 0 AND web_type = '{$update_arr['w_type']}' 
 				";
 
		$result = $GLOBALS['db']->query($sql);
		while ($row = $GLOBALS['db']->fetch_array($result)) {
			$ar_str_id .= ','.$row['ar_id'] ;
		}
		
		$ar_str_id = substr($ar_str_id, 1, str_len($ar_str_id));
		 	
		//定义需要修改（修改时间的表值对应数组）
		$table_arr = array(0 => array('table' => $rows['article']['table'], 'field_id' => $rows['article']['field_id'], 'id' => $ar_str_id),
					 	   1 => array('table' => $rows['tag']['table'], 'field_id' => $rows['tag']['field_id'], 'id' => $update_arr['tag_id']),
					 	   2 => array('table' => $rows['article_single']['table'], 'field_id' => $rows['article_single']['field_id'], 'id' => $ar_str_id),   	
		);
		
		//发布或修改(修改时间)相关联文章模板
		$this->operation_template($update_arr, $table_arr);
	}
	
	/**
	* 更新分类模板
	* @param array $update_arr 需要更新的数组
	* @return boolean
	* */
	function updateCategoryTemplate( $update_arr) {
		
		$rows = $GLOBALS['site_config'][$update_arr['w_type']];

		//为文章分类
		 if ($update_arr['category_type'] == 1) {
		 	
		 	//相关联分类id
		 	$sql = "SELECT cat_related FROM category  
					WHERE cat_id = '{$update_arr['category_id']}' ";
		 	$cat_related_id = $GLOBALS['db']->getOne($sql);
	
			if ( substr($cat_related_id, -1) == ',' ){
				$cat_related_id = substr($cat_related_id, 0, strlen($cat_related_id) - 1) ;
			} 
		 	
		 	$ar_str_id = '';//定义分类相关文章id
		 	if ($update_arr['cat_series'] == 3  ) {
		 		
		 		$sql = "SELECT ar_id FROM topic_article 
		 				WHERE ar_vicecat_id = '{$update_arr['category_id']}' AND is_delete = 0 AND web_type = '{$update_arr['w_type']}' 
		 				UNION ALL
		 				SELECT ar_id FROM topic_article 
		 				WHERE ar_cat_id = '{$update_arr['category_id']}' AND is_delete = 0 AND web_type = '{$update_arr['w_type']}' 
		 				";
		 		
				$result = $GLOBALS['db']->query($sql);
				
				while ($row = $GLOBALS['db']->fetch_array($result)) {
					$ar_str_id .= ','.$row['ar_id'] ;
				}
				
				$ar_str_id = substr($ar_str_id, 1, str_len($ar_str_id));
		 	}
		 
		 	//定义需要修改（修改时间的表值对应数组）
			$table_arr = array(
						 0 => array('table' => $rows['article']['table'], 'field_id' => $rows['article']['field_id'], 'id' => $ar_str_id),  
						 1 => array('table' => $rows['category']['table'], 'field_id' => $rows['category']['field_id'], 'id' => $update_arr['category_id']),  
						 2 => array('table' => $rows['category']['table'], 'field_id' => $rows['category']['field_id'], 'id' => $cat_related_id),
						 3 => array('table' => $rows['article_single']['table'], 'field_id' => $rows['article_single']['field_id'], 'id' => $ar_str_id),  
			);
		
		 	
		 } else if($update_arr['category_type'] == 2) { //为产品分类
		 	
		 	//相关联分类名称
		 	$sql = "SELECT cat_title FROM category  
					WHERE cat_id = '{$update_arr['category_id']}' ";
		 	$cat_title = $GLOBALS['db']->getOne($sql);
		 	
		 	$product_str_id = '';//定义分类相关产品id(won_product的主键)
		 	$product_str_name = '';//定义分类相关产品名称
		 	if ($update_arr['cat_series'] == 3  ) {
		 		
		 		$sql = "SELECT won_p.id, p.product_name FROM won_product won_p 
		 				LEFT JOIN product p ON p.product_id = won_p.won_product_id
		 				WHERE won_p.cat_id = '{$update_arr['category_id']}' AND won_p.won_product_delete = 0 AND won_p.web_type = '{$update_arr['w_type']}' ";
		 		
				$result = $GLOBALS['db']->query($sql);
				
				while ($row = $GLOBALS['db']->fetch_array($result)) {
					$product_str_id .= ','.$row['id'] ;
					$product_str_name .= ','.$row['product_name'] ;
				}
				
				$product_str_id = substr($product_str_id, 1, str_len($product_str_id));
				$product_str_name = substr($product_str_name, 1, str_len($product_str_name));
		 	}
		 	
		 	//定义需要修改（修改时间的表值对应数组）
			$table_arr = array(
						 0 => array('table' => $rows['product_category_one']['table'], 'field_id' => $rows['product_category_one']['field_id'], 'id' => trim($cat_title)),  
						 1 => array('table' => $rows['product_category_two']['table'], 'field_id' => $rows['product_category_two']['field_id'], 'id' => trim($cat_title)),  
						 2 => array('table' => $rows['store_category_one']['table'], 'field_id' => $rows['store_category_one']['field_id'], 'id' => $update_arr['category_id']),
						 3 => array('table' => $rows['store_category_two']['table'], 'field_id' => $rows['store_category_two']['field_id'], 'id' => $update_arr['category_id']),
			);
			
			if (!empty($product_str_id)) {
				$table_arr[4] = array('table' => $rows['product_buy']['table'], 'field_id' => $rows['product_buy']['field_id'], 'id' => trim($product_str_id));
				$table_arr[5] = array('table' => $rows['product_single_buy']['table'], 'field_id' => $rows['product_single_buy']['field_id'], 'id' => trim($product_str_id));
				$table_arr[6] = array('table' => $rows['product']['table'], 'field_id' => $rows['product']['field_id'], 'id' => trim($product_str_id));
				$table_arr[7] = array('table' => $rows['product_guide']['table'], 'field_id' => $rows['product_guide']['field_id'], 'id' => trim($product_str_id));
				
				$table_arr[8] = array('table' => $rows['product_uninstall_two']['table'], 'field_id' => $rows['product_uninstall_two']['field_id'], 'id' => trim($product_str_id));
				$table_arr[9] = array('table' => $rows['product_thankyou']['table'], 'field_id' => $rows['product_thankyou']['field_id'], 'id' => trim($product_str_id));
				$table_arr[10] = array('table' => $rows['product_uninstall_two']['table'], 'field_id' => $rows['product_uninstall_two']['field_id'], 'id' => trim($product_str_id));
				$table_arr[11] = array('table' => $rows['product_install']['table'], 'field_id' => $rows['product_install']['field_id'], 'id' => trim($product_str_name));
				$table_arr[12] = array('table' => $rows['product_uninstall_survey']['table'], 'field_id' => $rows['product_uninstall_survey']['field_id'], 'id' => trim($product_str_name));
			}
		
		 }
		 
		 //发布或修改(修改时间)相关联文章模板
		$this->operation_template($update_arr, $table_arr);
	}
	
	/**
	* 更新文章模板
	* @param array $update_arr 需要更新的数组
	* @return boolean
	* */
	function updateArticleTemplate( $update_arr) {

		$rows = $GLOBALS['site_config'][$update_arr['w_type']];
		
		$sql = "SELECT ar_title, ar_vicecat_id, ar_cat_id, ar_tag_id, related_article FROM topic_article 
					WHERE ar_id = '{$update_arr['ar_id']}' AND is_delete = 0 LIMIT 1";
		
		$article_arr = $GLOBALS['db']->getOneRs($sql);
		//更新title  
		$ar_title = addslashes($article_arr['ar_title']);
		$sql = "UPDATE Table_{$rows['article']['table']} SET Field_{$rows['article']['field_title']} = '{$ar_title}',	 
				MendTime = '{$_SERVER['REQUEST_TIME']}' , MenderID = '{$_SESSION['AID']}' 
				WHERE Field_{$rows['article']['field_id']} = '{$update_arr['ar_id']}' ";
		//die($sql);
		$affect =  $GLOBALS['db']->query( $sql );
		
		//文章单页模板更新
		if ($affect == 0 && !empty($rows['article_single'])){
			
			$sql = "UPDATE Table_{$rows['article_single']['table']} SET Field_{$rows['article_single']['field_title']} = '{$ar_title}',	 
					MendTime = {$_SERVER['REQUEST_TIME']} , MenderID = {$_SESSION['AID']} 
					WHERE Field_{$rows['article_single']['field_id']} = {$update_arr['ar_id']}";
			$GLOBALS['db']->query( $sql );
		}
		
		 //以下是得到分类所关联的文章ID
 		$sql = "SELECT ar_id 
				FROM topic_article ar , (
				SELECT ar_cat_id 
				FROM topic_article 
				WHERE ar_id = '{$update_arr['ar_id']}' AND is_delete = 0 LIMIT 1) a 				
				WHERE a.ar_cat_id = ar.ar_cat_id AND ar.is_delete = 0 AND ar.web_type = '{$update_arr['w_type']}'";
		$result = $GLOBALS['db']->query($sql);
		
		$ar_str_id = $update_arr['ar_id'];
		while ($row = $GLOBALS['db']->fetch_array($result)) {
			$ar_str_id .= ','.$row['ar_id'] ;
		}
		
		
		//定义需要修改（修改时间的表值对应数组）
		$table_arr = array(0 => array('table' => $rows['article']['table'], 'field_id' => $rows['article']['field_id'], 'id' => $ar_str_id),
					 1 => array('table' => $rows['article_single']['table'], 'field_id' => $rows['article_single']['field_id'], 'id' => $ar_str_id), 
					 2 => array('table' => $rows['article']['table'], 'field_id' => $rows['article']['field_id'], 'id' => $article_arr['related_article']),  
					 3 => array('table' => $rows['category']['table'], 'field_id' => $rows['category']['field_id'], 'id' => $article_arr['ar_cat_id']),  
					 4 => array('table' => $rows['category']['table'], 'field_id' => $rows['category']['field_id'], 'id' => $article_arr['ar_vicecat_id']),  
					 5 => array('table' => $rows['tag']['table'], 'field_id' => $rows['tag']['field_id'], 'id' => $article_arr['ar_tag_id']),
		);
		
		//发布或修改(修改时间)相关联文章模板
		$this->operation_template($update_arr, $table_arr);
		
		
	}
	
	
	/**
	* 更新产品模板
	* @param array $update_arr 需要更新的数组
	* @return boolean
	* */
	function updateProductsTemplate( $update_arr) {
		
		
			$rows = $GLOBALS['site_config'][$update_arr['w_type']];
			
			
			$sql = "SELECT cat.parent_path, p.product_rel, p.product_name,   won_p.high_product FROM won_product won_p 
					LEFT JOIN product p ON won_p.won_product_id = p.product_id  
					LEFT JOIN category cat ON cat.cat_id  = won_p.cat_id
					WHERE won_p.id = '{$update_arr['id']}' AND won_p.won_product_delete = 0 LIMIT 1";
			$product_arr = $GLOBALS['db']->getOneRs($sql);
			
			
			
			$high_recommended_product_id = '';//推荐产品和高版本id
			
			$sql = "SELECT won_p.id FROM won_product won_p 
					WHERE ( FIND_IN_SET('{$update_arr['id']}', recommended_product) OR won_p.id = '{$update_arr['high_product']}' ) AND won_p.won_product_delete = 0 ";
		
			$result = $GLOBALS['db']->query($sql);
			
			while ($row = $GLOBALS['db']->fetch_array($result)) {
				$high_recommended_product_id .= ','.$row['id'] ;
				
			}
			//修改store首页
			if (!empty($rows['store_index']['table'])) {
				
				$sql = "UPDATE Table_{$rows['store_index']['table']} SET MendTime = '{$_SERVER['REQUEST_TIME']}' ";
				$GLOBALS['db']->query($sql);
			}
		
			
			//定义需要修改（修改时间的表值对应数组）
			$table_arr = array(
						 0 => array('table' => $rows['product']['table'], 'field_id' => $rows['product']['field_id'], 'id' => $update_arr['id'].$high_recommended_product_id),
						 1 => array('table' => $rows['product_tab']['table'], 'field_id' => $rows['product_tab']['field_id'], 'id' => $update_arr['id'].$high_recommended_product_id), 
						 2 => array('table' => $rows['product_techspec']['table'], 'field_id' => $rows['product_techspec']['field_id'], 'id' => $update_arr['id'].$high_recommended_product_id),  
						 3 => array('table' => $rows['product_guide']['table'], 'field_id' => $rows['product_guide']['field_id'], 'id' => $update_arr['id'].$high_recommended_product_id),  
						 4 => array('table' => $rows['product_comment']['table'], 'field_id' => $rows['product_comment']['field_id'], 'id' => $update_arr['id'].$high_recommended_product_id),  
						 5 => array('table' => $rows['product_single']['table'], 'field_id' => $rows['product_single']['field_id'], 'id' => $update_arr['id'].$high_recommended_product_id),
						 6 => array('table' => $rows['product_buy']['table'], 'field_id' => $rows['product_buy']['field_id'], 'id' => $update_arr['id']),
						 7 => array('table' => $rows['product_single_buy']['table'], 'field_id' => $rows['product_single_buy']['field_id'], 'id' => $update_arr['id']),  
						 8 => array('table' => $rows['product_single']['table'], 'field_id' => $rows['product_single']['field_id'], 'id' => $update_arr['id']),  
						 
						 9 => array('table' => $rows['product_related_single']['table'], 'field_id' => $rows['product_related_single']['field_id'], 'id' => $update_arr['id']),
						 10 => array('table' => $rows['product_survey']['table'], 'field_id' => $rows['product_survey']['field_id'], 'id' => $product_arr['product_name']),
						 11 => array('table' => $rows['product_install']['table'], 'field_id' => $rows['product_install']['field_id'], 'id' => $product_arr['product_name']),  
						 12 => array('table' => $rows['product_uninstall_survey']['table'], 'field_id' => $rows['product_uninstall_survey']['field_id'], 'id' => $product_arr['product_name']),  
						 13 => array('table' => $rows['product_uninstall']['table'], 'field_id' => $rows['product_uninstall']['field_id'], 'id' => $update_arr['id']),  
						 14 => array('table' => $rows['product_uninstall_two']['table'], 'field_id' => $rows['product_uninstall_two']['field_id'], 'id' => $update_arr['id']),
						 15 => array('table' => $rows['product_thankyou']['table'], 'field_id' => $rows['product_thankyou']['field_id'], 'id' => $update_arr['id']), 
						  
						 16 => array('table' => $rows['product_single_comment']['table'], 'field_id' => $rows['product_single_comment']['field_id'], 'id' => $update_arr['id']),   
						 17 => array('table' => $rows['product_download_jump']['table'], 'field_id' => $rows['product_download_jump']['field_id'], 'id' => $update_arr['id']),     
						
			);
			
			
			//修改store一级分类 和二级分类模板
			if(!empty($product_arr['parent_path'])) {
				
				$row = explode(',', $product_arr['parent_path']);
				$table_arr['18'] = array('table' => $rows['store_category_one']['table'], 'field_id' => $rows['store_category_one']['field_id'], 'id' => $row[0]);
				$table_arr['19'] = array('table' => $rows['store_category_two']['table'], 'field_id' => $rows['store_category_two']['field_id'], 'id' => $row[1]); 
			}
			
			//修改store_list
			$sql = "SELECT store_cat_id FROM store
			 		WHERE store_product_id = '{$update_arr['id']}' AND store_product_delete = 0 LIMIT 1";
			$store_cat_id = $GLOBALS['db']->getOne($sql);
			
			if (!empty($store_cat_id)) {
				$table_arr['20'] = array('table' => $rows['store_list']['table'], 'field_id' => $rows['store_list']['field_id'], 'id' => $store_cat_id); 
			}
	
			//发布或修改(修改时间)相关联产品模板
			$this->operation_template($update_arr, $table_arr);
			unset($product_arr, $row);
		
	}
	
	/**
	* 更新所有产品模板
	* @param array $update_arr 需要更新的数组
	* @return boolean
	* */
	function updateAllProductsTemplate( $update_arr) {
		
			
			foreach ($GLOBALS['site_config'] AS $key => $rows) {
				
				if (!empty($rows['product'])) {
					
					$table_arr = array(
									 0 => array('table' => $rows['product']['table']),
									 1 => array('table' => $rows['product_tab']['table']), 
									 2 => array('table' => $rows['product_techspec']['table']),  
									 3 => array('table' => $rows['product_guide']['table']),  
									 4 => array('table' => $rows['product_comment']['table']),  
									 5 => array('table' => $rows['product_buy']['table']),
									 6 => array('table' => $rows['product_single_buy']['table']),
									 7 => array('table' => $rows['product_survey']['table']),  
									 8 => array('table' => $rows['product_install']['table']),  
									 9 => array('table' => $rows['product_uninstall_survey']['table']),  
									 10 => array('table' => $rows['product_uninstall']['table']),  
									 11 => array('table' => $rows['product_uninstall_two']['table']),
									 12 => array('table' => $rows['product_thankyou']['table']),  
									 
									 13 => array('table' => $rows['store_category_one']['table']),  
									 14 => array('table' => $rows['store_category_two']['table']),  
									 15 => array('table' => $rows['product_single']['table']),
									 
									 16 => array('table' => $rows['product_single_comment']['table']),  
									 17 => array('table' => $rows['product_download_jump']['table']),  
									
									
									
									
					);
					
					foreach ($table_arr AS $key1 => $rows1) {
						if (!empty($rows1['table'])) {
							$sql = "UPDATE Table_{$rows1['table']} SET MendTime = '{$_SERVER['REQUEST_TIME']}' ";
							$GLOBALS['db']->query($sql);
						}
					}
					unset($rows1);
					
				}
			}
			
			unset($rows);
			
	
	}
	

	
	/**
	* 操作模板
	* @param array $update_arr 需要更新的数组
	* @param array $table_arr 需要更新的模板表
	* @return boolean
	* */
	
	function operation_template($update_arr, $table_arr) {
		
		//发布相关联模板
	
		if($update_arr['type'] == 'pub') {
			
			foreach ($table_arr AS $key => $values) {
				if (!empty($values['table']) && !empty($values['field_id']) && !empty($values['id'])) {
					
					pub_template($values['table'], trim($values['id']), 'Field_'.$values['field_id'], 'pub' );
				}
			}
			
		} else if($update_arr['type'] == 'update') {//只修改模板修改时间
					 
			foreach ($table_arr AS $key => $values) {
				
				if (!empty($values['table']) && !empty($values['field_id']) && !empty($values['id'])) {
					
					pub_template($values['table'], trim($values['id']), 'Field_'.$values['field_id'], 'update' );
				}
			}
			
		
		}
	}
	
	/**
	* 更新模板
	* @param array $update_arr 需要更新的数组
	* @return boolean
	* */
	function updateTemplate( $update_arr){
		
		if($update_arr['operation_type'] == 'product' ) {
			
			$this->updateProductsTemplate( $update_arr);
			
		} else if($update_arr['operation_type'] == 'article') {
			
			$this->updateArticleTemplate( $update_arr);
			
		} else if($update_arr['operation_type'] == 'category') {
		
			$this->updateCategoryTemplate( $update_arr);
			
		} else if($update_arr['operation_type'] == 'tag') {
			
			$this->updateTagTemplate( $update_arr);
			
		} else if($update_arr['operation_type'] == 'all_product') {
			
			$this->updateAllProductsTemplate( $update_arr);
			
		} else if($update_arr['operation_type'] == 'author') {
			
			$this->updateAuthorTemplate( $update_arr);
		} 
	}


}
?>